import requests
import time,random
from hashlib import md5

class YDSpider():
    def __init__(self):
        self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

        self.headers = {
            Your headres!
        }

    def parse_parameter(self,word):
        lts = str(int(time.time()*1000))
        salt = lts + str(random.randint(0,9))
        str_ = "fanyideskweb" + word + salt + "Tbh5E8=q6U3EXe+&L[4c@"
        md = md5()
        md.update(str_.encode())
        sign = md.hexdigest()

        return lts,salt,sign
    def get_parameter(self,word):
        lts,salt,sign = self.parse_parameter(word)
        data = {
            'i': word,
            'from': 'AUTO',
            'to':' AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': salt,
            'sign': sign,
            'lts': lts,
            'bv': 'cda1e53e0c0eb8dd4002cefc117fa588',
            'doctype': 'json',
            'version':'2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTlME'
        }

        resp = requests.post(url=self.url,data = data,headers=self.headers)
        json = resp.json()
        text = json['translateResult'][0][0]['tgt']
        print(text)

    def main(self):
        word = input('请输入:')
        self.get_parameter(word)

YDSpider().main()